package com.xjrsoft.module.customerTwo.AppManage.service_support_order_service.service.impl;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjrsoft.core.tool.utils.StringUtil;
import com.xjrsoft.module.customerTwo.AppManage.service_support_order_service.constants.OpinionConstants;
import com.xjrsoft.module.customerTwo.AppManage.service_support_order_service.constants.OrderStateConstants;
import com.xjrsoft.module.customerTwo.AppManage.service_support_order_service.entity.ServiceSupportOrder;


import java.time.LocalDateTime;
import java.util.ArrayList;
import com.xjrsoft.module.customerTwo.AppManage.service_support_order_service.entity.ServiceSupportOrderTable;
import com.xjrsoft.module.customerTwo.AppManage.service_support_order_service.service.IServiceSupportOrderTableService;
import com.xjrsoft.module.customerTwo.AppManage.service_support_order_service.entity.ServiceSupportOrderTableDetails;
import com.xjrsoft.module.customerTwo.AppManage.service_support_order_service.service.IServiceSupportOrderTableDetailsService;
import com.xjrsoft.module.customerTwo.AppManage.service_support_order_service.entity.ServiceSupportService;
import com.xjrsoft.module.customerTwo.AppManage.service_support_order_service.service.IServiceSupportServiceService;
import com.xjrsoft.module.customerTwo.AppManage.service_support_order_service.dto.ServiceSupportOrderListDto;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.page.ConventPage;

import java.util.List;


import com.xjrsoft.module.customerTwo.AppManage.service_support_order_service.mapper.ServiceSupportOrderMapper;
import com.xjrsoft.module.customerTwo.AppManage.service_support_order_service.service.IServiceSupportOrderService;
import com.xjrsoft.core.mp.base.BaseService;
import com.xjrsoft.core.tool.utils.Func;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *  服务实现类
 *
 * @author lisen
 * @since 2024-04-26
 */
@Service
@AllArgsConstructor
public class ServiceSupportOrderServiceImpl extends BaseService<ServiceSupportOrderMapper, ServiceSupportOrder> implements IServiceSupportOrderService {

	private IServiceSupportOrderTableService serviceSupportOrderTableService;
	private IServiceSupportOrderTableDetailsService serviceSupportOrderTableDetailsService;
	private IServiceSupportServiceService serviceSupportServiceService;

	@Override
	public IPage<ServiceSupportOrder> getPageList(ServiceSupportOrderListDto pageListDto) {
		Wrapper<ServiceSupportOrder> wrapper = Wrappers.<ServiceSupportOrder>query().lambda()
				.like(!StringUtil.isEmpty(pageListDto.getOrder_number()), ServiceSupportOrder::getOrderNumber, pageListDto.getOrder_number())
				.ge(!StringUtil.isEmpty(pageListDto.getCreate_date_Start()), ServiceSupportOrder::getCreateDate, pageListDto.getCreate_date_Start())
				.le(!StringUtil.isEmpty(pageListDto.getCreate_date_End()), ServiceSupportOrder::getCreateDate, pageListDto.getCreate_date_End())
				.eq(!StringUtil.isEmpty(pageListDto.getOrder_id()), ServiceSupportOrder::getOrderId, pageListDto.getOrder_id())
				.like(!StringUtil.isEmpty(pageListDto.getService_provider_id()), ServiceSupportOrder::getServiceProviderId, pageListDto.getService_provider_id())
				.like(!StringUtil.isEmpty(pageListDto.getCustomer_id()), ServiceSupportOrder::getCustomerId, pageListDto.getCustomer_id())
				.like(!StringUtil.isEmpty(pageListDto.getCustomer_phone()), ServiceSupportOrder::getCustomerPhone, pageListDto.getCustomer_phone())
				.like(!StringUtil.isEmpty(pageListDto.getState()), ServiceSupportOrder::getState, pageListDto.getState())
				.like(!StringUtil.isEmpty(pageListDto.getOpinion()), ServiceSupportOrder::getOpinion, pageListDto.getOpinion())
				.like(!StringUtil.isEmpty(pageListDto.getClosed_date()), ServiceSupportOrder::getClosedDate, pageListDto.getClosed_date());
		return this.page(ConventPage.getPage(pageListDto), wrapper);
	}

	/**
	* 客户新增售后需求
	*/
	@Override
	@Transactional
	public Integer addServiceSupportOrder(ServiceSupportOrder serviceSupportOrder, ServiceSupportOrderTable serviceSupportOrderTable, List<ServiceSupportOrderTableDetails> serviceSupportOrderTableDetailsList) {
		serviceSupportOrder.setState(OrderStateConstants.TODO);
		boolean isSuccess = this.save(serviceSupportOrder);
		serviceSupportOrderTable.setOrderId(serviceSupportOrder.getId());
		isSuccess &= serviceSupportOrderTableService.save(serviceSupportOrderTable);
		serviceSupportOrderTableDetailsList.forEach(serviceSupportOrderTableDetails -> serviceSupportOrderTableDetails.setOrderTableId(serviceSupportOrderTable.getId()));
		serviceSupportOrderTableDetailsList.forEach(serviceSupportOrderTableDetails -> serviceSupportOrderTableDetails.setOrderId(serviceSupportOrder.getId()));
		isSuccess &= serviceSupportOrderTableDetailsService.saveBatch(serviceSupportOrderTableDetailsList);
		return isSuccess ? serviceSupportOrder.getId() : null;
	}

	/***
	 商户处理
	*/
	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean addServiceSupportservice(Integer id,ServiceSupportService serviceSupportService) {
		ServiceSupportOrder serviceSupportOrder = getById(id);
		//工单转为处理中
		serviceSupportOrder.setState(OrderStateConstants.HANDLING);
		serviceSupportService.setOrderId(id);
		return this.updateById(serviceSupportOrder)&&
				serviceSupportServiceService.save(serviceSupportService);

	}

	/**
	 * 客户评价
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean opinion(ServiceSupportOrder serviceSupportOrder) {
		serviceSupportOrder.setState(OrderStateConstants.CLOSED);
		serviceSupportOrder.setClosedDate(LocalDateTime.now());
		return this.updateById(serviceSupportOrder);
	}


	//更新整个工单，非法的权限
	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean updateServiceSupportOrder(ServiceSupportOrder serviceSupportOrder,List<ServiceSupportOrderTable> serviceSupportOrderTables){
		serviceSupportOrderTables.forEach(i->i.setOrderId(serviceSupportOrder.getId()));
		List<ServiceSupportOrderTableDetails> serviceSupportOrderTableDetails = new ArrayList<>();
		serviceSupportOrderTables.forEach(i->{
			List<ServiceSupportOrderTableDetails> tableDetails = i.getServiceSupportOrderTableDetailsList();
			tableDetails.forEach(o->o.setOrderTableId(i.getId()));
			tableDetails.forEach(o->o.setOrderId(serviceSupportOrder.getId()));
			serviceSupportOrderTableDetails.addAll(tableDetails);
		});
		return this.updateById(serviceSupportOrder)&&
				serviceSupportOrderTableService.updateBatchById(serviceSupportOrderTables)&&
				serviceSupportOrderTableDetailsService.updateBatchById(serviceSupportOrderTableDetails);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean delete(String ids) {
		List<String> idList = Func.toStrList(ids);
		if (this.removeByIds(idList)) {
			serviceSupportOrderTableService.remove(Wrappers.<ServiceSupportOrderTable>query().lambda().in(ServiceSupportOrderTable::getOrderId, ids));
			serviceSupportOrderTableDetailsService.remove(Wrappers.<ServiceSupportOrderTableDetails>query().lambda().in(ServiceSupportOrderTableDetails::getOrderId, ids));
			serviceSupportServiceService.remove(Wrappers.<ServiceSupportService>query().lambda().in(ServiceSupportService::getOrderId, ids));
			return true;
		}
		return false;
	}

	@Override
	public List<ServiceSupportOrderTable> getServiceSupportOrderTableByParentId(Integer parentId){
		Wrapper wrapper = Wrappers.<ServiceSupportOrderTable>query().lambda().eq(ServiceSupportOrderTable::getOrderId, parentId);
		return serviceSupportOrderTableService.list(wrapper);
	}
	@Override
	public List<ServiceSupportOrderTableDetails> getServiceSupportOrderTableDetailsByParentId(Integer parentId){
		Wrapper wrapper = Wrappers.<ServiceSupportOrderTableDetails>query().lambda().eq(ServiceSupportOrderTableDetails::getOrderId, parentId);
		return serviceSupportOrderTableDetailsService.list(wrapper);
	}
	@Override
	public List<ServiceSupportService> getServiceSupportServiceByParentId(Integer parentId){
		Wrapper wrapper = Wrappers.<ServiceSupportService>query().lambda().eq(ServiceSupportService::getOrderId, parentId);
		return serviceSupportServiceService.list(wrapper);
	}

	/*public ServiceSupportService getUnChecked(Integer parentId){
		Wrapper wrapper = Wrappers.<ServiceSupportService>query().lambda().eq(ServiceSupportService::getOrderId, parentId).eq(ServiceSupportService::getIsChecked,0);
		return serviceSupportServiceService.getOne(wrapper);
	}*/
}